package com.en.effectiveJava.chapter2.noCreateSameObject5;

import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

/**
 * 检验一个人是否出生于1946年至1964年期间
 * Create by EN
 * On 30/05/18 1:00 PM
*/
public class Person {

    private final Date birthDate;

    public Person(Date birthDate) {
        this.birthDate = birthDate;
    }

    /**
     * 判断该人是否出生于生育高峰期
     * 每次执行都会新建一些可重用的对象，不要这样做
     * @return
     */
    /*public boolean isBadyBoomer() {
        Calendar gmtCal = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
        gmtCal.set(1946, Calendar.JANUARY, 1, 0, 0, 0);
        Date boomStart = gmtCal.getTime();
        gmtCal.set(1965, Calendar.JANUARY, 1, 0, 0, 0);
        Date boomEnd = gmtCal.getTime();

        return birthDate.compareTo(boomStart) >= 0 &&
               birthDate.compareTo(boomEnd)   <  0;
    }*/

}

class Person2 {

    private final Date birthDate;

    public Person2(Date birthDate) {
        this.birthDate = birthDate;
    }

    /**
     * 生育高峰期的开始时间和结束时间
     */
    public static final Date BOOM_START;
    public static final Date BOOM_END;

    static {
        Calendar gmtCal = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
        gmtCal.set(1946, Calendar.JANUARY, 1, 0, 0, 0);
        BOOM_START = gmtCal.getTime();
        gmtCal.set(1965, Calendar.JANUARY, 1, 0, 0, 0);
        BOOM_END = gmtCal.getTime();
    }

    public boolean isBadyBoomer() {
        return birthDate.compareTo(BOOM_START) >= 0 &&
               birthDate.compareTo(BOOM_END)   <  0;
    }



}
